import sys

import xlwings as xw

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget

from Class import mainClass
from Class.mainClass import Warehouse, Goods, InboundOrders, OutboundOrders

from GUI import Gui, mGui
from GUI.MangerPage import addPage, selectPage, updataPage
from GUI.Page_1 import pageUi_1
from GUI.Page_2 import pageUi_2
from GUI.Page_3 import pageUi_3


# 初始化对象
app = QApplication(sys.argv)
wh = Warehouse()


# 分页面 添加
def createAddPage(data):
    addPageUi = QWidget()
    addPageUi.setWindowTitle("Add Page")
    addP = addPage.Ui_Form()
    addP.setupUi(addPageUi)
    addP.setText()
    addPageUi.show()
    addP.pushButton.clicked.connect(lambda: addPageUi.close())
    if data == wh.Goods:
        addP.pushButton.clicked.connect(lambda: wh.addGood(addP.getData()))
    if data == wh.InboundOrders:
        addP.pushButton.clicked.connect(lambda: wh.addInboundOrders(addP.getData()))
    if data == wh.OutboundOrders:
        addP.pushButton.clicked.connect(lambda: wh.addOutboundOrders(addP.getData()))
    addP.pushButton.clicked.connect(lambda: pui_1.importData(wh.Goods, len(wh.Goods)))
    addP.pushButton.clicked.connect(lambda: pui_2.importData(wh.InboundOrders, len(wh.InboundOrders)))
    addP.pushButton.clicked.connect(lambda: pui_3.importData(wh.OutboundOrders, len(wh.OutboundOrders)))


# 分页面 查找
def createSecPage(type):
    selPage = QWidget()
    selPage.setWindowTitle("Warehouse Management System")
    sp = selectPage.Ui_Form()
    sp.setupUi(selPage)
    selPage.show()
    if type == wh.Goods:
        sp.pushButton.clicked.connect(lambda: sp.selGoods())
    if type == wh.InboundOrders:
        sp.pushButton.clicked.connect(lambda: sp.selInboundOrders())
    if type == wh.OutboundOrders:
        sp.pushButton.clicked.connect(lambda: sp.selOutboundOrders())
    sp.pushButton.clicked.connect(lambda: selPage.close)


# 分页面 删除
def createDelPage(goal, data):
    QDial = QtWidgets.QDialog()
    QDial.setWindowTitle("删除")
    QDial.setGeometry(QtCore.QRect(700, 420, 500, 150))
    text = QtWidgets.QTextBrowser(QDial)
    text.setText("确认删除？？？")
    text.setGeometry(QtCore.QRect(150, 20, 200, 50))
    text.setReadOnly(True)
    button = QtWidgets.QPushButton(QDial)
    button.setGeometry(QtCore.QRect(200, 100, 100, 20))
    button.setText("OK")
    button.setObjectName("OK")
    button.clicked.connect(QDial.accept)
    row = goal.tableWidget.currentRow()
    if data == wh.Goods:
        button.clicked.connect(lambda: wh.deleteGood(goal.tableWidget.item(row, 0).text()))
    if data == wh.InboundOrders:
        button.clicked.connect(lambda: wh.deleteInboundOrders(goal.tableWidget.item(row, 0).text()))
    if data == wh.OutboundOrders:
        button.clicked.connect(lambda: wh.deleteOutboundOrders(goal.tableWidget.item(row, 0).text()))
    button.clicked.connect(lambda: pui_1.importData(wh.Goods, len(wh.Goods)))
    button.clicked.connect(lambda: pui_2.importData(wh.InboundOrders, len(wh.InboundOrders)))
    button.clicked.connect(lambda: pui_3.importData(wh.OutboundOrders, len(wh.OutboundOrders)))
    QDial.show()
    QDial.exec_()


# 分页面 修改
def createUpdatePage(goal):
    updP = QWidget()
    updP.setWindowTitle("Warehouse Management System")
    up = updataPage.Ui_Form()
    up.setupUi(updP)
    updP.show()
    up.pushButton.clicked.connect(lambda: updP.close())
    row = goal.tableWidget.currentRow()
    up.textEdit.setText(goal.tableWidget.item(row, 0).text())
    up.textEdit.setReadOnly(True)
    up.textEdit_2.setText(goal.tableWidget.item(row, 1).text())
    up.textEdit_3.setText(goal.tableWidget.item(row, 2).text())
    up.textEdit_4.setText(goal.tableWidget.item(row, 3).text())
    up.textEdit_5.setText(goal.tableWidget.item(row, 4).text())

    def getData():
        good = Goods(int(up.textEdit.toPlainText()),
                     str(up.textEdit_2.toPlainText()),
                     float(up.textEdit_3.toPlainText()),
                     float(up.textEdit_4.toPlainText()),
                     int(up.textEdit_5.toPlainText()))
        return good

    up.pushButton.clicked.connect(lambda: wh.updateGood(getData()))
    up.pushButton.clicked.connect(lambda: pui_1.importData(wh.Goods, len(wh.Goods)))
    up.pushButton.clicked.connect(lambda: pui_2.importData(wh.InboundOrders, len(wh.InboundOrders)))
    up.pushButton.clicked.connect(lambda: pui_3.importData(wh.OutboundOrders, len(wh.OutboundOrders)))


def out():
    with xw.App(visible=True, add_book=False) as app:
        book = app.books.add()

        sht = book.sheets.active
        sht.range("A1").value = "ID"
        sht.range("B1").value = "Name"
        sht.range("C1").value = "InPrice"
        sht.range("D1").value = "OutPrice"
        sht.range("E1").value = "Inventory"
        sht.range("A1", "E1").color = (178, 255, 255)
        for i in wh.Goods:
            sht.range(i.id + 1, 1).value = str(i.id)
            sht.range(i.id + 1, 2).value = str(i.name)
            sht.range(i.id + 1, 3).value = str(i.inPrice)
            sht.range(i.id + 1, 4).value = str(i.outPrice)
            sht.range(i.id + 1, 5).value = str(i.inventory)
        sht.autofit()

        sht2 = book.sheets.add()
        sht2.range("A1").value = "ID"
        sht2.range("B1").value = "Name"
        sht2.range("C1").value = "InTime"
        sht2.range("D1").value = "InQuantity"
        sht2.range("A1", "D1").color = (178, 255, 255)
        for i in wh.InboundOrders:
            sht2.range(i.id + 1, 1).value = str(i.id)
            sht2.range(i.id + 1, 2).value = str(i.name)
            sht2.range(i.id + 1, 3).value = str(i.intime)
            sht2.range(i.id + 1, 4).value = str(i.inquantity)
        sht2.autofit()

        sht3 = book.sheets.add()
        sht3.range("A1").value = "ID"
        sht3.range("B1").value = "Name"
        sht3.range("C1").value = "OutTime"
        sht3.range("D1").value = "OutQuantity"
        sht3.range("A1", "D1").color = (178, 255, 255)
        for i in wh.OutboundOrders:
            sht3.range(i.id + 1, 1).value = str(i.id)
            sht3.range(i.id + 1, 2).value = str(i.name)
            sht3.range(i.id + 1, 3).value = str(i.outtime)
            sht3.range(i.id + 1, 4).value = str(i.outquantity)
        sht3.autofit()

        book.save("DATA/data.xls")
        book.close()


def unOut():
    mainClass.clearDataBase()
    with xw.App(visible=False, add_book=False) as app:
        book = app.books.open("DATA/data.xls")
        sht = book.sheets("sheet1")
        var = sht.range("A1").expand().value
        sht2 = book.sheets("sheet2")
        var2 = sht2.range("A1").expand().value
        sht3 = book.sheets("sheet3")
        var3 = sht3.range("A1").expand().value
        for i in var[1:]:
            newgood = Goods(int(i[0]), str(i[1]), float(i[2]), float(i[3]), int(i[4]))
            wh.addGood(newgood)
        for i in var2[1:]:
            newIn = InboundOrders(int(i[0]), str(i[1]), str(i[2]), int(i[3]))
            wh.addInboundOrders(newIn)
        for i in var3[1:]:
            newOut = OutboundOrders(int(i[0]), str(i[1]), str(i[2]), int(i[3]))
            wh.addOutboundOrders(newOut)
        book.close()
    wh.flashWH()
    exit(0)


if __name__ == '__main__':
    # 欢迎页面
    welcome = QWidget()
    welcome.setWindowTitle("Warehouse Management System")
    ui = Gui.Ui_Form()
    ui.setupUi(welcome)
    welcome.show()
    timer = QtCore.QTimer()
    timer.timeout.connect(welcome.close)
    timer.timeout.connect(lambda: mainUI.show())
    timer.start(2000)

    # 主页面
    mainUI = QWidget()
    mainUI.setWindowTitle("Warehouse Management System")
    mui = mGui.Ui_Form()
    mui.setupUi(mainUI)
    mui.pushButton.clicked.connect(lambda: pageUI_1.show())
    mui.pushButton_2.clicked.connect(lambda: pageUI_2.show())
    mui.pushButton_3.clicked.connect(lambda: pageUI_3.show())

    # 分页面1 货物
    pageUI_1 = QWidget()
    pageUI_1.setWindowTitle("Goods Management System")
    pui_1 = pageUi_1.Ui_Form()
    pui_1.setupUi(pageUI_1)
    pui_1.importData(wh.Goods, len(wh.Goods))

    pui_1.pushButton_3.clicked.connect(lambda: createAddPage(wh.Goods))
    pui_1.pushButton.clicked.connect(lambda: createSecPage(wh.Goods))
    pui_1.pushButton_4.clicked.connect(lambda: createDelPage(pui_1, wh.Goods))
    pui_1.pushButton_2.clicked.connect(lambda: createUpdatePage(pui_1))

    # 分页面2 入库订单
    pageUI_2 = QWidget()
    pageUI_2.setWindowTitle("InboundOrders Management System")
    pui_2 = pageUi_2.Ui_Form()
    pui_2.setupUi(pageUI_2)
    pui_2.importData(wh.InboundOrders, len(wh.InboundOrders))

    pui_2.pushButton_4.clicked.connect(lambda: createDelPage(pui_2, wh.InboundOrders))
    pui_2.pushButton.clicked.connect(lambda: createSecPage(wh.InboundOrders))
    pui_2.pushButton_2.clicked.connect(lambda: out())
    pui_2.pushButton_3.clicked.connect(lambda: unOut())

    # 分页面3 出库订单
    pageUI_3 = QWidget()
    pageUI_3.setWindowTitle("OutboundOrders Management System")
    pui_3 = pageUi_3.Ui_Form()
    pui_3.setupUi(pageUI_3)
    pui_3.importData(wh.OutboundOrders, len(wh.OutboundOrders))

    pui_3.pushButton_4.clicked.connect(lambda: createDelPage(pui_3, wh.OutboundOrders))
    pui_3.pushButton.clicked.connect(lambda: createSecPage(wh.OutboundOrders))
    pui_3.pushButton_2.clicked.connect(lambda: out())
    pui_3.pushButton_3.clicked.connect(lambda: unOut())

    exit(app.exec_())
